package com.bank.dao;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class DBUtil {
    private static final String URL = "jdbc:mysql://localhost:3306/数据库课设?useUnicode=true&characterEncoding=UTF-8&useSSL=false&serverTimezone=UTC";
    private static final String USER = "root";
    private static final String PASSWORD = "123456";

    static {
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            System.out.println("【DBUtil】MySQL JDBC 驱动程序已加载");
        } catch (ClassNotFoundException e) {
            System.err.println("【DBUtil】错误: MySQL JDBC 驱动程序未找到！");
            e.printStackTrace();
            System.exit(1);
        }
    }

    public static Connection getConnection() throws SQLException {
        System.out.println("\n【DBUtil】获取数据库连接...");
        System.out.println("【DBUtil】URL: " + URL);
        System.out.println("【DBUtil】User: " + USER);

        Connection conn = DriverManager.getConnection(URL, USER, PASSWORD);
        System.out.println("【DBUtil】连接成功! 连接对象: " + conn);

        // 打印数据库元数据
        try {
            System.out.println("【DBUtil】数据库产品: " + conn.getMetaData().getDatabaseProductName());
            System.out.println("【DBUtil】数据库版本: " + conn.getMetaData().getDatabaseProductVersion());
        } catch (SQLException e) {
            System.err.println("【DBUtil】无法获取数据库元数据: " + e.getMessage());
        }

        return conn;
    }
}